const { ccclass, property } = cc._decorator;

@ccclass
export default class timerControl extends cc.Component {
    @property(cc.Label)
    label: cc.Label = null;
    @property
    warningTime: number = 10;
    @property
    timeing: number = 0;
    @property({
        type: [cc.Component.EventHandler],
        tooltip: "计时结束"
    })
    timerEvents: Array<cc.Component.EventHandler> = [];
    startTime: number = 120;

    start() {
        this.init();
        this.schedule(this.updateTime, 1);
    }
    init() {
        this.startTime = HeroManager.getInstance().playTime;
        this.timeing = this.startTime;
        this.label.string = this.startTime.toString();
    }
    updateTime() {
        this.timeing--;
        this.label.string = this.timeing.toString();
        if (this.timeing == this.warningTime) {
            this.label.node.color = cc.color(255, 0, 0);
        }
        if (this.timeing == 0) {
            cc.Component.EventHandler.emitEvents(this.timerEvents, {
                target: this,
                timeing: this.timeing
            });
            this.unschedule(this.updateTime);
        }
    }
}
